﻿using System.Collections.Generic;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Basmus.Tests
{
    public static class ContainerHelper
    {
        public static IEnumerable<T[]> GetContainersOfArray<T>()
        {
            return ElementsHelper.GetElementsOf<T>().Select(x => x == null ? null : x.ToArray<T>());
        }

        public static IEnumerable<List<T>> GetContainersOfList<T>()
        {
            return ElementsHelper.GetElementsOf<T>().Select(x => x == null ? null : x.ToList<T>());
        }

        public static IEnumerable<T> GetContainersOfEntity<T>()
        {
            return ElementsHelper.GetElementsOf<T>().Where(x => x != null).SelectMany(x => x);
        }

        public static IEnumerable<T?> GetContainersOfNullable<T>()
            where T : struct
        {
            return GetContainersOfEntity<T>()
                .Select(x => (T?) x)
                .Concat(new T?[] {null});
        }

        public static TCollection GenerateCollectionContainers<TCollection, TElement>(IEnumerable<TElement> elements) where TCollection : class, IEnumerable<TElement>
        {
            if (elements == null)
            {
                return null;
            }
            else if (typeof(TCollection).IsArray && typeof(TCollection).GetElementType() == typeof(TElement))
            {
                object obj = elements.ToArray();
                return (TCollection)obj;
            }
            else if (typeof(TCollection) == typeof(List<TElement>) || typeof(TCollection) == typeof(IList<TElement>))
            {
                object obj = elements.ToList();
                return (TCollection)obj;
            }
            else if (typeof(TCollection) == typeof(IEnumerable<TElement>))
            {
                object obj = elements;
                return (TCollection)obj;
            }

            Assert.Fail();
            return null;
        }
    }
}